﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace CAUNetLogin.Util.SystemLib
{
    public class Registery
    {
        public static string lastError;
        public static bool isException = false;
        public Registery()
        {

        }

        public static bool LMSetRegistery(string keyPath, string keyName, string keyValue)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine.CreateSubKey(keyPath);
                runKey.SetValue(keyName, keyValue);
                runKey.Close();
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                return false;
            }
            return true;
        }

        public static bool LMDeleteRegistery(string keyPath, string keyName)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine.CreateSubKey(keyPath);
                runKey.DeleteValue(keyName);
                runKey.Close();
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                return false;
            }
            return true;
        }

        public static bool LMCheckRegistery(string keyPath, string keyName, string keyValue)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine;
                using (RegistryKey startKey = runKey.CreateSubKey(keyPath))
                {
                    string[] subStrings = startKey.GetValueNames();
                    foreach (string _keyName in subStrings)
                    {
                        if (_keyName == keyName)
                        {
                            string str = (string)(startKey.GetValue(keyName));
                            if (str.Equals(keyValue))
                            {
                                return true;
                            }
                            return false;
                        }
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                isException = true;
                return false;
            }

        }


        public static bool LMCheckRegistery(string keyPath, string keyName)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine;
                using (RegistryKey startKey = runKey.CreateSubKey(keyPath))
                {
                    string[] subStrings = startKey.GetValueNames();
                    foreach (string _keyName in subStrings)
                    {
                        if (_keyName == keyName)
                        {
                            return true;
                        }
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                isException = true;
                return false;
            }

        }


        public static bool CUSetRegistery(string keyPath, string keyName, string keyValue)
        {
            try
            {
                RegistryKey runKey = Registry.CurrentUser.CreateSubKey(keyPath);
                runKey.SetValue(keyName, keyValue);
                runKey.Close();
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                return false;
            }
            return true;
        }

        public static bool CUDeleteRegistery(string keyPath, string keyName)
        {
            try
            {
                RegistryKey runKey = Registry.CurrentUser.CreateSubKey(keyPath);
                runKey.DeleteValue(keyName);
                runKey.Close();
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                return false;
            }
            return true;
        }

        public static bool CUCheckRegistery(string keyPath, string keyName, string keyValue)
        {
            RegistryKey runKey = Registry.CurrentUser;
            using (RegistryKey startKey = runKey.CreateSubKey(keyPath))
            {
                string[] subStrings = startKey.GetValueNames();
                foreach (string _keyName in subStrings)
                {
                    if (_keyName == keyName)
                    {
                        string str = (string)(startKey.GetValue(keyName));
                        if (str.Equals(keyValue))
                        {
                            return true;
                        }
                        return false;
                    }
                }
                return false;
            }
        }
    }
}
